<?php

namespace App\Listeners;

use App\Models\Car;
use App\Models\MyCar;
use Carbon\Carbon;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

/**
 * 系统赠送车辆 - 赠送用户一辆默认车
 * Class SystemGiftListener
 * @package App\Listeners
 */
class SystemGiftListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  object  $event
     * @return void
     */
    public function handle($event)
    {
        $user = $event->user;
        $cars = $event->cars;
        if(!$cars)return;
        if(!is_array($cars))$cars=[$cars];
        Car::forSell()->whereIn('CarID',$cars)->get()->map(function($item)use($user){
            $new_car = new MyCar();
            $new_car->insert([
                'uid'=>$user->id,
                'CarId'=>$item->id,
                'BuyTime'=>Carbon::now()->toDateTimeString(),
                'BuyCredit'=>0,
            ]);
        });
        
    }
}
